// 定义菜单项接口
interface MenuItem {
  id: number | string;
  parentId: number | string;
  children?: MenuItem[];
  [key: string]: any; // 允许其他动态属性
}

// 处理菜单,将其转化为tree结构
export function changeTree(menus: any[], id: number | string): MenuItem[] {
  return [...menus]
    .filter((res) => res.parentId == id)
    .map((item) => {
      item.children = changeTree(menus, item.id);
      return item;
    });
}

// 定义排列组合函数的泛型类型
export function combineArrays<T>(arrays: T[][]): T[][] {
  const result = arrays.reduce((acc: T[][], currArr: T[]) => {
    if (acc.length === 0) {
      return currArr.map((item) => [item]);
    } else {
      const temp: T[][] = [];
      for (let i = 0; i < acc.length; i++) {
        for (let j = 0; j < currArr.length; j++) {
          // 修复点：直接展开 acc[i]，无需类型判断
          const num: T[] = [...acc[i], currArr[j]];
          temp.push(num);
        }
      }
      return temp;
    }
  }, [] as T[][]);

  return result;
}

// 通过name获取对象数组的对象值
export function getObjectValueByName<T>(arr: T[], name: string, str: string): T | undefined {
  return arr.find((item: any) => item[str] === name);
}
